package com.groupfive.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.groupfive.common.Result;
import com.groupfive.feign.client.DeptClient;
import com.groupfive.pojo.Dept;
import com.groupfive.pojo.Employee;
import com.groupfive.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/web")
public class WebController {

    @Autowired
    private DeptClient deptClient;
    @Autowired
    private EmployeeService employeeService;

    @GetMapping("/barData")
    public Result getBarData(){
        //List<Dept> deptList = deptService.selectAll(null);
        JSONObject jsonObject = JSONUtil.parseObj(deptClient.selectAll());
        Object data = jsonObject.get("data");
        List<Dept> deptList = JSONUtil.toList(data.toString(), Dept.class);

        List<Employee> employeeList = employeeService.selectAll(null);
        for (Employee employee : employeeList){
            for (Dept dept : deptList){
                if (dept.getId().equals(employee.getDeptId())){
                    employee.setDeptName(dept.getName());
                }
            }
        }

        List<Long> countList = new ArrayList<>();
        for (Dept dept : deptList){
            long count = employeeList.stream().filter(employee -> dept.getName().equals(employee.getDeptName())).count();
            countList.add(count);
        }

        List<String> deptNameList = new ArrayList<>();
        for (Dept dept : deptList){
            deptNameList.add(dept.getName());
        }

        Map<String,  Object> resMap =new HashMap<>();
        resMap.put("deptName", deptNameList);
        resMap.put("count", countList);
        return Result.success(resMap);
    }

    @GetMapping("/pieData")
    public Result getPieData(){
        //List<Dept> deptList = deptService.selectAll(null);
        JSONObject jsonObject = JSONUtil.parseObj(deptClient.selectAll());
        Object data = jsonObject.get("data");
        List<Dept> deptList = JSONUtil.toList(data.toString(), Dept.class);

        List<Employee> employeeList = employeeService.selectAll(null);
        for (Employee employee : employeeList){
            for (Dept dept : deptList){
                if (dept.getId().equals(employee.getDeptId())){
                    employee.setDeptName(dept.getName());
                }
            }
        }

        List<Map<String, Object>> pieList = new ArrayList<>();

        for (Dept dept : deptList){
            String deptName = dept.getName();
            long count = employeeList.stream().filter(employee -> deptName.equals(employee.getDeptName())).count();
            System.out.println(deptName + ":" + count);
            Map<String, Object> resMap = new HashMap<>();
            resMap.put("value", count);
            resMap.put("name", deptName);
            pieList.add(resMap);
        }

        return Result.success(pieList);
    }
}
